package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2023/10/10 09:57
 * @apiNote
 */

public class Test00457CircularArrayLoop {
    public static void main(String[] args) {
        int[] nums = new int[]{2, -1, 1, 2, 2};
        System.out.println(test(nums));

    }

    public static boolean test(int[] nums) {
        for(int i = 0; i < nums.length; i++){
            int slow = i;
            int fast = next(nums,i);
            while(nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(nums,fast)] > 0){
                if(slow == fast){
                    if(slow != next(nums,slow)){
                        return true;
                    }else{
                        break;
                    }
                }
                slow = next(nums,slow);
                fast = next(nums,next(nums,fast));
            }
        }
        return false;
    }

    public static int next(int[] nums,int i){
        int n = nums.length;
        return ((i+nums[i]) % n + n) % n;
    }

}
